package com.ja.openchat.adapters;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.ja.openchat.ui.me.MyAlbumFragment;
import com.ja.openchat.views.UserAvatarView;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.circle.BlogReplyInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;
import com.zy.openchat.util.ComplexSpannableStringUtils;
import com.zy.openchat.util.TimeUtils;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;

public class CircleInnerRemarkAdapter extends BaseQuickAdapter<BlogReplyInfo, BaseViewHolder> {
    private BlogReplyInfo mParentReplyInfo = null;
    private BaseViewModel viewModel;

    public CircleInnerRemarkAdapter(@Nullable List<BlogReplyInfo> data, BlogReplyInfo parentReply, BaseViewModel viewModel) {
        super(R.layout.item_circle_remark_inner, data);
        mParentReplyInfo = parentReply;
        this.viewModel = viewModel;
        addChildClickViewIds(R.id.inner_remark_btn, R.id.like_btn);
    }

    private BlogReplyInfo findReplyById(long id) {
        if (mParentReplyInfo == null) {
            return null;
        }
        if (mParentReplyInfo.reply_id == id) {
            return mParentReplyInfo;
        }
        if (Common.isEmpty(mParentReplyInfo.replys)) {
            return null;
        }
        for (BlogReplyInfo blogReplyInfo : mParentReplyInfo.replys) {
            if (blogReplyInfo.id == id) {
                return blogReplyInfo;
            }
        }
        return null;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, BlogReplyInfo blogReplyInfo) {
        UserInfo userInfo = UserInfo.getUserInfo(blogReplyInfo.user_id);
        UserAvatarView iv_icon = helper.getView(R.id.user_image);
        ComplexSpannableStringUtils.Builder stringBuilder;
        if (userInfo != null) {
            if (userInfo.profile_photo != null && !userInfo.profile_photo.isSmallPhotoDownloaded()) {
                blogReplyInfo.isAvaterDownloaded = false;
            } else {
                blogReplyInfo.isAvaterDownloaded = true;
            }

            stringBuilder = ComplexSpannableStringUtils.getBuilder(userInfo.displayName());
            iv_icon.showUser(userInfo);
        } else {
            iv_icon.showDefault();
            blogReplyInfo.isAvaterDownloaded = false;
            TelegramManager.getTelegramManager().getUserSimpleInfo(blogReplyInfo.user_id);
            stringBuilder = ComplexSpannableStringUtils.getBuilder(" ");
        }

        BlogReplyInfo replyInfo = findReplyById(blogReplyInfo.reply_id);
        if (replyInfo != null && replyInfo.user_id != 0) {
            stringBuilder.append(String.format(" %s ",getContext().getString(R.string.reply_text))).setForegroundColor(Color.parseColor("#5A6C92"));
            UserInfo replyUserInfo = UserInfo.getUserInfo(replyInfo.user_id);
            if (replyUserInfo != null) {
                stringBuilder.append(replyUserInfo.displayName());
            } else {
                TelegramManager.getTelegramManager().getUserSimpleInfo(replyInfo.user_id);
            }
        }

        helper.setText(R.id.user_name_text, stringBuilder.create());
        helper.setText(R.id.post_time_text, TimeUtils.getMsgFormatTime(blogReplyInfo.date * 1000));
        helper.getView(R.id.like_btn).setActivated(blogReplyInfo.liked);
        helper.setText(R.id.like_btn, blogReplyInfo.likeNumber() == 0 ? "" : String.valueOf(blogReplyInfo.likeNumber()));
        helper.setText(R.id.remark_content, blogReplyInfo.text);
        helper.getView(R.id.user_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();
                bundle.putLong("userId", blogReplyInfo.user_id);
                viewModel.startContainerActivity(MyAlbumFragment.class.getCanonicalName(), bundle);
            }
        });
    }
}
